<template>
	<view>
		<view class="header status_bar" :class="scrrol == true ? 'scrrolHeader' : ''">
			<view class="back iconfont icon-xiangzuo1" @tap="backPage"></view>
			<view class="text" :class="scrrol == true ? 'scrrolHeader' : ''">搜索</view>
		</view>	
		<view class="search_maxbox" :class="scrrol == true ? 'scrrolHeader' : ''">
			<view class="search_box">
				<view class="search">
					<view class="icon iconfont icon-sousuo"></view>
					<input type="text" value="" v-model="keyword" placeholder="搜索您要找的车" />
					<view class="menu" @tap="doSearch(false)">搜索</view>
				</view>
			</view>
		</view>
		<view class="clear_box"></view>
		<view class="clear_box02"></view>
		
		<view class="page_title">
			<view class="title">历史记录</view>
			<!-- <view class="arrow iconfont icon-xiangyou1"></view> -->
			<i class="iconfont icon" @tap="oldDelete" >&#xe622;</i>
		</view>
		<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
			<block v-for="(row,index) in keywordList" :key="index">
				<view class="keyword-entry" hover-class="keyword-entry-tap" >
					<view class="keyword-text" @tap.stop="doSearch(keywordList[index].keyword,keywordList[index].id)">
						<rich-text :nodes="row.htmlStr"></rich-text>
					</view>
					
					<view class="keyword-img" @tap.stop="setKeyword(keywordList[index].keyword)">
						<image src="/static/HM-search/back.png"></image>
					</view>
					
				</view>
			</block>
		
		</scroll-view>
		<view class="historyItem">
			<view class="keywordItem" v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index" >
				{{keyword}}
			</view>
			<view class="keywordItem" v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index" >
				{{keyword}}
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page:1,
				scrrol:false,
				info:'',
				stype : 0,
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				forbid: '',
				isShowKeywordList: false
			}
		},
		onLoad(opt) {
			this.stype = opt.stype||0;
			this.loadDefaultKeyword();
			this.loadOldKeyword();
		},
		methods: {
			setRule(e){
				let that = this;
				let index = e.currentTarget.dataset.index;
				that.flowList[index].tick = !that.flowList[index].tick
			},
			blur(){
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "默认关键字";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			loadHotKeyword() {
				//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
				this.hotKeywordList = ['哎咪啦热能文胸贴', '热能腰带', '阴阳贴', '热能小芯片', '哎呦Ｖ'];
			}, 
			//监听输入
			inputChange(event) {
				//兼容引入组件时传入参数情况
				var keyword = event.detail?event.detail.value:event;
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				//以下示例截取淘宝的关键字，请替换成你的接口
				let url = this.site_url + 'api/index/search_car_keyword?keyword=' + keyword +'&stype='+this.stype;
				this.areq(url).then(res=>{
					   if(res.code == 1){
						   this.keywordList = [];
						   this.keywordList = this.drawCorrelativeKeyword(res.data, keyword);
					   }
				})
			},
			// 高亮关键字
			drawCorrelativeKeyword(keywords, keyword) {
				var len = keywords.length,
					keywordArr = [];
				for (var i = 0; i < len; i++) {
					var row = keywords[i];
					//定义高亮#9f9f9f
					var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
					html = '<div>' + html + '</div>';
					var tmpObj = {
						keyword: row[0],
						id :  row[1],
						htmlStr: html
					};
					keywordArr.push(tmpObj)
				}
				return keywordArr;
			},
			//顶置关键字
			setKeyword(index) {
				this.keyword = this.keywordList[index].keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			// 执行搜索
			doSearch(keyword,id=0) {
				keyword = keyword===false?this.keyword:keyword;
				if(keyword == ''){
					this.Msg.Fail('请输入关键字');
					return;
				}
				this.keyword = keyword;
				this.saveKeyword(keyword); // 保存为历史 
				uni.showToast({
					title: keyword,
					icon: 'none',
					duration: 2000
				});
				let url = '/pages/index/list?keyword='+keyword+"&id="+id;
				uni.navigateTo({
					url: url
				});
			},
			// 保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			},
			backPage() {
				uni.navigateBack({});
			},
			gotopage(e) {
				let url = e.currentTarget.dataset.url;
				uni.navigateTo({
					url: url
				});
			},

		},
		
	}
</script>

<style>
	
	.header{
		background-color: transparent !important;
	}
	
	.header .text{
		background-color: transparent !important;
		color: #333 !important;
	}
	
	.header .back{
		color: #333 !important;
	}
	
	.scrrolHeader{
		background-color: #F9E9E3 !important;
	}
	
	.clear_box02 {
		width: 100%;
		height: calc(12vw + var(--status-bar-height));
		;
	}
	
	.search_maxbox {
		width: 100%;
		padding: 3vw 4%;
		position: fixed;
		left: 0;
		top: calc(13.5vw + var(--status-bar-height));
		z-index: 98;
	}
	
	.search_box {
		width: 100%;
		border-radius: 2vw;
		border: 1px solid #FC5D20;
		display: flex;
		align-items: center;
		height: 12vw;
		padding-left: 4vw;
	}
	
	.search_box .city_box {
		display: flex;
		align-items: center;
		padding: 0 2vw;
		border-right: 1px solid #E8E8F0;
		margin-right: 2vw;
	}
	
	.search_box .city_box .city {
		font-size: 3.8vw;
		color: #979AA9;
		margin-right: 1vw;
	}
	
	.search_box .city_box .arrow {
		font-size: 3vw;
		color: #979AA9;
	}
	
	.search_box .search {
		flex: 1;
		display: flex;
		align-items: center;
	}
	
	.search_box .search .icon {
		font-size: 4vw;
		color: #979AA9;
		margin-right: 2vw;
	}
	
	.search_box .search input {
		flex: 1;
		font-size: 3.5vw;
		color: #333;
	}
	
	.search_box .search .menu {
		font-size: 4vw;
		height: 9vw;
		width: 16vw;
		color: #fff;
		background-color: #FE811E;
		border-radius: 2vw;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 1.5vw;
	}
	
	.page_title{
		width: 92%;
		margin: 4vw auto 0;
		display: flex;
		align-items: center;
	}
	
	.page_title .title{
		font-size: 4.5vw;
		color: #333;
		font-weight: bold;
		flex: 1;
	}
	
	.page_title .arrow{
		font-size: 4vw;
		color: #999;
	}
	
	.historyItem{
		width: calc(100% + 30rpx);
	}
	.keywordItem{
		padding: 10rpx 15rpx;
		display: inline-block;
		font-size: 30rpx;
		color: #707070;
		background: #FFFFFF;
		border-radius: 30rpx;
		margin-top: 25rpx;
		margin-left: 30rpx;
	}
	.placeholder-class {color:#9e9e9e;}
	.search-keyword {width:100%;background-color:rgb(242,242,242);}
	.keyword-list-box {height:calc(100vh - 110upx);padding-top:10upx;border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-entry-tap {background-color:#eee;}
	.keyword-entry {width:94%;height:80upx;margin:0 3%;font-size:30upx;color:#333;display:flex;justify-content:space-between;align-items:center;border-bottom:solid 1upx #e7e7e7;}
	.keyword-entry image {width:60upx;height:60upx;}
	.keyword-entry .keyword-text,.keyword-entry .keyword-img {height:80upx;display:flex;align-items:center;}
	.keyword-entry .keyword-text {width:90%;}
	.keyword-entry .keyword-img {width:10%;justify-content:center;}
	.keyword-box {height:calc(100vh - 110upx);border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-box .keyword-block {padding:10upx 0;}
	.keyword-box .keyword-block .keyword-list-header {width:94%;padding:10upx 3%;font-size:27upx;color:#333;display:flex;justify-content:space-between;}
	.keyword-box .keyword-block .keyword-list-header image {width:40upx;height:40upx;}
	.keyword-box .keyword-block .keyword {width:94%;padding:3px 3%;display:flex;flex-flow:wrap;justify-content:flex-start;}
	.keyword-box .keyword-block .hide-hot-tis {display:flex;justify-content:center;font-size:28upx;color:#6b6b6b;}
	.keyword-box .keyword-block .keyword>view {display:flex;justify-content:center;align-items:center;border-radius:60upx;padding:0 20upx;margin:10upx 20upx 10upx 0;height:60upx;font-size:28upx;background-color:rgb(242,242,242);color:#6b6b6b;}
	
	
	
</style>
